#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student {
 char name[20];
 int age;
 char sex;
};
 
int main(int argc,const char * argv[])
{
 //使⽤fopen 打开⼀个⽂件
 FILE * fp = fopen("./user.text","r");//注意 ，加双引号
 if(fp == NULL)
 {
 printf("⽂件打开失败\n");
 return 0;
 }
 struct student stu1 = {"yangshuaige",18,'m'};
 //向终端输⼊⼀个字符串
 //fprintf(fp,"学⽣的名字:%s,学⽣的年龄:%d,性d别:%c\n",stu1.name,stu1.age,stu1.sex);
 //使⽤fscanf向⽂件中获取数据
 char name[20];
 int age;
 char sex;
 //fscanf(fp,"学⽣的名字:%s,学⽣的年龄:%d,性别:%c",name,&age,&sex);
 //fscanf如果有%s 会把后⾯的全部当成字符串
 //使⽤fgets获得⼀⾏数据，然会对其进⾏分割
 char temp[128];
 fgets(temp,128,fp);//读取⼀⾏数据，处理数据
 strcpy(name,strtok(temp,":"));
 //age = *(strtok(NULL,":"));
 //函数 atoi 将数字型型字符串 转化为整型数据
 age = atoi(strtok(NULL,":"));
 sex = *(strtok(NULL,":"));
 printf("名字:%s\n",name);
 printf("名字:%d\n",age);
 printf("名字:%c\n",sex);
 //关闭⽂件指针，关闭之后，就不能通过该⽂件指针对⽂件操作了
 if(fclose(fp) == -1)
 {
 printf("⽂件关闭失败\n");
 }
 return 0;
}